<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>检查一个变量是否为空</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.doubleval.html">doubleval</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.floatval.html">floatval</a></div>
 <div class="up"><a href="ref.var.html">Variable handling 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="function.empty" class="refentry">
   <div class="refnamediv">
    <h1 class="refname">empty</h1>
    <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">empty</span> &mdash; <span class="dc-title">检查一个变量是否为空</span></p>

   </div>
 <div class="refsect1 description" id="refsect1-function.empty-description">
  <h3 class="title">说明</h3>
     <div class="methodsynopsis dc-description">
      <span class="type">bool</span> <span class="methodname"><strong>empty</strong></span>
       ( <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$var</code></span>
      )</div>

    <p class="para rdfs-comment">
     如果 <em><code class="parameter">var</code></em> 是非空或非零的值，则
     <span class="function"><strong>empty()</strong></span> 返回 
     <strong><code>FALSE</code></strong>。换句话说，<em>&quot;&quot;</em>、<em>0</em>、<em>&quot;0&quot;</em>、<strong><code>NULL</code></strong>、<strong><code>FALSE</code></strong>、<em>array()</em>、<em>var $var;</em>
     以及没有任何属性的对象都将被认为是空的，如果
     <em><code class="parameter">var</code></em> 为空，则返回 <strong><code>TRUE</code></strong>。
    </p>
    <p class="para">     
     除了当变量没有置值时不产生警告之外，<span class="function"><strong>empty()</strong></span> 是
     <em>(boolean) var</em>
     的反义词。参见<a href="language.types.boolean.html#language.types.boolean.casting" class="link">转换为布尔值</a>获取更多信息。
    </p>
    <p class="para">
     <div class="example" id="example-5150">
      <p><strong>Example #1 
       <span class="function"><strong>empty()</strong></span> 与 <span class="function"><a href="function.isset.html" class="function">isset()</a></span>
       的一个简单比较。
      </strong></p>  
      <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$var&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;结果为&nbsp;true，因为&nbsp;$var&nbsp;为空<br /></span><span style="color: #007700">if&nbsp;(empty(</span><span style="color: #0000BB">$var</span><span style="color: #007700">))&nbsp;{&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'$var&nbsp;is&nbsp;either&nbsp;0&nbsp;or&nbsp;not&nbsp;set&nbsp;at&nbsp;all'</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;结果为&nbsp;false，因为&nbsp;$var&nbsp;已设置<br /></span><span style="color: #007700">if&nbsp;(!isset(</span><span style="color: #0000BB">$var</span><span style="color: #007700">))&nbsp;{&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'$var&nbsp;is&nbsp;not&nbsp;set&nbsp;at&nbsp;all'</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
      </div>

     </div>
    </p>

    <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">因为是一个语言构造器而不是一个函数，不能被
<a href="functions.variable-functions.html" class="link">可变函数</a> 调用。
</span>
</p></blockquote>
    
    <blockquote class="note"><p><strong class="note">Note</strong>: 
     <p class="para">
      <span class="function"><strong>empty()</strong></span> 只检测变量，检测任何非变量的东西都将导致解析错误。换句话说，后边的语句将不会起作用：
      <strong class="command">empty(addslashes($name))</strong>。
     </p>
    </p></blockquote>
    <p class="simpara">
     参见 <span class="function"><a href="function.isset.html" class="function">isset()</a></span>、<span class="function"><a href="function.unset.html" class="function">unset()</a></span>、<span class="function"><a href="function.array-key-exists.html" class="function">array_key_exists()</a></span>、<span class="function"><a href="function.count.html" class="function">count()</a></span>
     和 <span class="function"><a href="function.strlen.html" class="function">strlen()</a></span>。
    </p>    
   </div>

 <div class="refsect1 parameters" id="refsect1-function.empty-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<em><code class="parameter">var</code></em></dt>

     <dd>

      <p class="para">
       Variable to be checked
      </p>
      <blockquote class="note"><p><strong class="note">Note</strong>: 
       <p class="para">
        <span class="function"><strong>empty()</strong></span> only checks variables as anything else will
        result in a parse error. In other words, the following will not work:
        <strong class="command">empty(trim($name))</strong>.
       </p>
      </p></blockquote>
      <p class="para">
       <span class="function"><strong>empty()</strong></span> is the opposite of
       <em>(boolean) var</em>,
       except that no warning is generated when the variable is not set.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.empty-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Returns <strong><code>FALSE</code></strong> if <em><code class="parameter">var</code></em> has a non-empty
   and non-zero value.
  </p>
  <p class="para">
   The following things are considered to be empty:
   <ul class="simplelist">
    <li class="member"><em>&quot;&quot;</em> (an empty string)</li>
    <li class="member"><em>0</em> (0 as an integer)</li>
    <li class="member"><em>0.0</em> (0 as a float)</li>
    <li class="member"><em>&quot;0&quot;</em> (0 as a string)</li>
    <li class="member"><strong><code>NULL</code></strong></li>
    <li class="member"><strong><code>FALSE</code></strong></li>
    <li class="member"><em>array()</em> (an empty array)</li>
    <li class="member"><em>var $var;</em> (a variable declared, but without a value in a class)</li>
   </ul>
  </p>
 </div>

 <div class="refsect1 changelog" id="refsect1-function.empty-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>5.4.0</td>
       <td>
        <p class="para">
         Checking non-numeric offsets of strings returns <strong><code>TRUE</code></strong>.
        </p>
       </td>
      </tr>

      <tr>
       <td>5.0.0</td>
       <td>
        <p class="para">
         Objects with no properties are no longer considered empty.
        </p>
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-function.empty-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-5151">
    <p><strong>Example #2 
     A simple <span class="function"><strong>empty()</strong></span> / <span class="function"><a href="function.isset.html" class="function">isset()</a></span>
     comparison.
    </strong></p>  
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$var&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;Evaluates&nbsp;to&nbsp;true&nbsp;because&nbsp;$var&nbsp;is&nbsp;empty<br /></span><span style="color: #007700">if&nbsp;(empty(</span><span style="color: #0000BB">$var</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'$var&nbsp;is&nbsp;either&nbsp;0,&nbsp;empty,&nbsp;or&nbsp;not&nbsp;set&nbsp;at&nbsp;all'</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;Evaluates&nbsp;as&nbsp;true&nbsp;because&nbsp;$var&nbsp;is&nbsp;set<br /></span><span style="color: #007700">if&nbsp;(isset(</span><span style="color: #0000BB">$var</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'$var&nbsp;is&nbsp;set&nbsp;even&nbsp;though&nbsp;it&nbsp;is&nbsp;empty'</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
  <div class="example" id="example-5152">
   <p><strong>Example #3 <span class="function"><strong>empty()</strong></span> on String Offsets</strong></p>
   <div class="example-contents"><p>
    PHP 5.4 changes how <span class="function"><strong>empty()</strong></span> behaves
    when passed string offsets.
   </p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$expected_array_got_string&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'somestring'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(empty(</span><span style="color: #0000BB">$expected_array_got_string</span><span style="color: #007700">[</span><span style="color: #DD0000">'some_key'</span><span style="color: #007700">]));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(empty(</span><span style="color: #0000BB">$expected_array_got_string</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(empty(</span><span style="color: #0000BB">$expected_array_got_string</span><span style="color: #007700">[</span><span style="color: #DD0000">'0'</span><span style="color: #007700">]));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(empty(</span><span style="color: #0000BB">$expected_array_got_string</span><span style="color: #007700">[</span><span style="color: #0000BB">0.5</span><span style="color: #007700">]));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(empty(</span><span style="color: #0000BB">$expected_array_got_string</span><span style="color: #007700">[</span><span style="color: #DD0000">'0.5'</span><span style="color: #007700">]));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(empty(</span><span style="color: #0000BB">$expected_array_got_string</span><span style="color: #007700">[</span><span style="color: #DD0000">'0&nbsp;Mostel'</span><span style="color: #007700">]));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程在PHP 5.3中的输出：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
</pre></div>
   </div>
   <div class="example-contents"><p>以上例程在PHP 5.4中的输出：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
bool(true)
bool(false)
bool(false)
bool(false)
bool(true)
bool(true)
</pre></div>
   </div>
  </div>
 </div>

 <div class="refsect1 notes" id="refsect1-function.empty-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">因为是一个语言构造器而不是一个函数，不能被
<a href="functions.variable-functions.html" class="link">可变函数</a> 调用。
</span>
</p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    When using <span class="function"><strong>empty()</strong></span> on inaccessible object properties,
    the <a href="language.oop5.overloading.html#object.isset" class="link">__isset()</a>
    overloading method will be called, if declared.
   </p>
  </p></blockquote>
 </div>

 <div class="refsect1 seealso" id="refsect1-function.empty-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.isset.html" class="function" rel="rdfs-seeAlso">isset()</a> - 检测变量是否设置</span></li>
    <li class="member"><a href="language.oop5.overloading.html#object.isset" class="link">__isset()</a></li>
    <li class="member"><span class="function"><a href="function.unset.html" class="function" rel="rdfs-seeAlso">unset()</a> - 释放给定的变量</span></li>
    <li class="member"><span class="function"><a href="function.array-key-exists.html" class="function" rel="rdfs-seeAlso">array_key_exists()</a> - 检查给定的键名或索引是否存在于数组中</span></li>
    <li class="member"><span class="function"><a href="function.count.html" class="function" rel="rdfs-seeAlso">count()</a> - 计算数组中的单元数目或对象中的属性个数</span></li> 
    <li class="member"><span class="function"><a href="function.strlen.html" class="function" rel="rdfs-seeAlso">strlen()</a> - 获取字符串长度</span></li>
    <li class="member"><a href="types.comparisons.html" class="link">The type comparison tables</a></li>
   </ul>
  </p>
 </div>

  </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.doubleval.html">doubleval</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.floatval.html">floatval</a></div>
 <div class="up"><a href="ref.var.html">Variable handling 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
